package com.lb.jwitter.action.report;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;

import com.lb.jwitter.service.CommonService;
import com.lb.jwitter.service.ConditionReport;
import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNam;
import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNamService;
import com.lb.jwitter.service.TblQLDTTXALichKiemTraGK;
import com.lb.jwitter.service.TblQLDTTXALichKiemTraGKService;
import com.lb.jwitter.service.TblQLDTTXALichThi;
import com.lb.jwitter.service.TblQLDTTXALichThiService;
import com.lb.jwitter.service.TblQLDTTXALopMonHoc;
import com.lb.jwitter.service.TblQLDTTXALopMonHocHocVienService;
import com.lb.jwitter.service.TblQLDTTXALopMonHocService;
import com.lb.jwitter.service.TblQuyenChiTietService;
import com.lb.jwitter.service.TblTaiKhoan;
import com.lb.jwitter.util.ApplicationContextProvider;
import com.lb.jwitter.util.Message;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

public class TblQLDTTXALichThiReportAction {
    private Message msg; 
    public Message getMsg() { return msg; }

    private Long hocKyId;
    
   public Long getHocKyId() {
		return hocKyId;
	}
	public void setHocKyId(Long hocKyId) {
		this.hocKyId = hocKyId;
	}
	
	private String loaiThi;
   
    public String getLoaiThi() {
		return loaiThi;
	}
	public void setLoaiThi(String loaiThi) {
		this.loaiThi = loaiThi;
	}

	private List<Integer> soLuongSVs;
	
	public List<Integer> getSoLuongSVs() {
		return soLuongSVs;
	}
	public void setSoLuongSVs(List<Integer> soLuongSVs) {
		this.soLuongSVs = soLuongSVs;
	}
	private List<TblQLDTTXALichThi> tblQLDTTXALichThis;
	private List<TblQLDTTXALichKiemTraGK> tblQLDTTXALichKiemTraGKs;

	private List<TblQLDTTXAHocKyTrongNam> tblQLDTTXAHocKyTrongNams;

	public List<TblQLDTTXAHocKyTrongNam> getTblQLDTTXAHocKyTrongNams() {
		return tblQLDTTXAHocKyTrongNams;
	}

	private TblQLDTTXAHocKyTrongNamService tblQLDTTXAHocKyTrongNamService;
    
  private TblTaiKhoan taiKhoanDangNhap; 
 public TblTaiKhoan getTaiKhoanDangNhap() { 
     return taiKhoanDangNhap; 
 } 
 public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
     this.taiKhoanDangNhap = taiKhoanDangNhap; 
 } 
 private TblQuyenChiTietService tblQuyenChiTietService;
	 @Autowired // Injected by Spring 
     ApplicationContextProvider contextProvider; 
 public String form() throws Exception 
{ 
 try{ 	
     Map session = ActionContext.getContext().getSession();
     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
     if( taiKhoanDangNhap == null) return "noprivilege";
    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_QLDTTXA_LopMonHoc", "xem")){ 
        return "noprivilege"; 
    } 
 
    tblQLDTTXAHocKyTrongNamService = (TblQLDTTXAHocKyTrongNamService) context.getBean("tbl_QLDTTXA_HocKyTrongNamClient"); 
        


        msg = new Message(); 
        List<ConditionReport> cons = new ArrayList<ConditionReport>();
        
        { 
            ConditionReport con = new ConditionReport(); 
            con.setCol("id");
            con.setValue1("desc");
            con.setOperator("order");
            cons.add(con);	
        } 
        
       tblQLDTTXAHocKyTrongNams = tblQLDTTXAHocKyTrongNamService.findtblQLDTTXAHocKyTrongNamByCondition(cons, 0, 0);
        
        
        
        return Action.SUCCESS;
	 }catch(Exception e){ 
	     System.out.println("Loi Report tblQLDTTXALopMonHoc Action"); 
	     e.printStackTrace(); 
	     return Action.ERROR; 
	}
}
 
 
 
 
 public String execute() throws Exception 
 { 
  try{ 	
      Map session = ActionContext.getContext().getSession();
      taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
      if( taiKhoanDangNhap == null) return "noprivilege";
     ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
     tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
     if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_QLDTTXA_LopMonHoc", "xem")){ 
         return "noprivilege"; 
     } 
     	tblQLDTTXAHocKyTrongNamService = (TblQLDTTXAHocKyTrongNamService) context.getBean("tbl_QLDTTXA_HocKyTrongNamClient");
     	CommonService commonService = (CommonService) context.getBean("commonClient");
     	TblQLDTTXALichKiemTraGKService tblQLDTTXALichKiemTraGKService = (TblQLDTTXALichKiemTraGKService) context.getBean("tbl_QLDTTXA_LichKiemTraGKClient");
     	TblQLDTTXALichThiService tblQLDTTXALichThiService = (TblQLDTTXALichThiService) context.getBean("tbl_QLDTTXA_LichThiClient");
     	TblQLDTTXALopMonHocHocVienService tblQLDTTXALopMonHocHocVienService = (TblQLDTTXALopMonHocHocVienService) context.getBean("tbl_QLDTTXA_LopMonHoc_HocVienClient");
        
     	
     	soLuongSVs = new ArrayList<Integer>();
         msg = new Message();
         List < ConditionReport > cons = new ArrayList<ConditionReport>(); 
         cons.clear();
         {            	 
        	ConditionReport con = new ConditionReport(); 
    	    con.setCol("lopId.hocKyId.id"); 
    	    con.setValue1(hocKyId); 
    	    con.setOperator("eq"); 
    	    cons.add(con);
         }
         { 
             ConditionReport con = new ConditionReport(); 
             con.setCol("ngayThi");
             con.setValue1("asc");
             con.setOperator("order");
             cons.add(con);	
         } 
         { 
             ConditionReport con = new ConditionReport(); 
             con.setCol("tietThiId.id");
             con.setValue1("asc");
             con.setOperator("order");
             cons.add(con);	
         } 
         if(loaiThi != null && loaiThi.equalsIgnoreCase("GK")){
        	 tblQLDTTXALichKiemTraGKs = tblQLDTTXALichKiemTraGKService.findtblQLDTTXALichKiemTraGKByCondition(cons, 0, 0);
        	 for(TblQLDTTXALichKiemTraGK lt : tblQLDTTXALichKiemTraGKs){
            	 cons.clear();
                 { 
                     ConditionReport con = new ConditionReport(); 
                     con.setCol("lopMonHocId.id");
                     con.setValue1(lt.getLopId().getId());
                     con.setOperator("eq");
                     cons.add(con);	
                 } 
                 int soLuongSV = tblQLDTTXALopMonHocHocVienService.counttblQLDTTXALopMonHocHocVienByCondition(cons);
                 soLuongSVs.add(new Integer(soLuongSV));
             }
         } else {
        	 tblQLDTTXALichThis = tblQLDTTXALichThiService.findtblQLDTTXALichThiByCondition(cons, 0, 0);
        	 for(TblQLDTTXALichThi lt : tblQLDTTXALichThis){
            	 cons.clear();
                 { 
                     ConditionReport con = new ConditionReport(); 
                     con.setCol("lopMonHocId.id");
                     con.setValue1(lt.getLopId().getId());
                     con.setOperator("eq");
                     cons.add(con);	
                 } 
                 int soLuongSV = tblQLDTTXALopMonHocHocVienService.counttblQLDTTXALopMonHocHocVienByCondition(cons);
                 soLuongSVs.add(new Integer(soLuongSV));
             }
         }

         
		  
		  return Action.SUCCESS; 
      } 
      catch(Exception e){ 
         System.out.println("Loi Report tblQLDTTXALopMonHoc Action"); 
         e.printStackTrace(); 
         return Action.ERROR; 
 } 
 }

public List<TblQLDTTXALichThi> getTblQLDTTXALichThis() {
	return tblQLDTTXALichThis;
}
public void setTblQLDTTXALichThis(List<TblQLDTTXALichThi> tblQLDTTXALichThis) {
	this.tblQLDTTXALichThis = tblQLDTTXALichThis;
}
public List<TblQLDTTXALichKiemTraGK> getTblQLDTTXALichKiemTraGKs() {
	return tblQLDTTXALichKiemTraGKs;
}
public void setTblQLDTTXALichKiemTraGKs(
		List<TblQLDTTXALichKiemTraGK> tblQLDTTXALichKiemTraGKs) {
	this.tblQLDTTXALichKiemTraGKs = tblQLDTTXALichKiemTraGKs;
}
public void setMsg(Message msg) {
	this.msg = msg;
} 



}
